<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改申请')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: bootstrap-select-css"/>
    <th:block th:include="include :: summernote-css"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-apply-edit" th:object="${apply}">
        <input id="id" name="id" th:field="*{id}" type="hidden">
        <div class="form-group">
            <label class="col-sm-2 control-label">标题：</label>
            <div class="col-sm-10">
                <input id="title" name="title" th:field="*{title}" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">申请人：</label>
            <div class="col-sm-10">
                <input id="applicant" name="applicant" th:field="*{applicant}" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">内容：</label>
            <div class="col-sm-10">
                <textarea id="content" th:field="*{content}" class="form-control" rows="6" name="content"></textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">附件：</label>
            <div class="col-sm-10">
                <input id="filePath" name="file" type="file" multiple class="file-upload form-control "/>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: summernote-js"/>
<script type="text/javascript">
    var prefix = ctx + "zzuser/apply";
    var fileName = "";
    var url = "";
    var file = "";

    function submitHandler() {
        var val = $("#form-apply-edit").serialize();
        console.log(val);
        if ($.validate.form()) {
            uploadFile();
            console.log("file:" + file);
            console.log("fileName:" + fileName);
            console.log("url:" + url);

            var fromData = $("#form-apply-edit").serialize();
            console.log("fromData:" + fromData);

            $.operate.save(prefix + "/edit", fromData + "&fileName=" + fileName + "&url=" + url);
        }
    }

    //文件上传
    function uploadFile() {
        var formData = new FormData();
        if ($('#filePath')[0].files[0] == null) {
            return false;
        }
        file = $('#filePath')[0].files[0].name;
        formData.append('fileName', $("#filePath").val());
        formData.append('file', $('#filePath')[0].files[0]);
        $.ajax({
            url: ctx + "common/upload",
            type: 'post',
            cache: false,
            data: formData,
            async: false,    //同步
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (result) {
                /* $.operate.successCallback(result);*/
                fileName = result.fileName;
                url = result.url;
                if (result) {
                    parent.$.modal.msgSuccess(result.msg);
                }
            }
        });
    }

</script>
</body>
</html>